import React from 'react'
import { Select, Tooltip } from 'antd'
export default {
    pagination(data, callback) {
        return {
        onChange: current => {
            callback(current)
        },
        current: data.pageNo,
        pageSize: data.pageSize,
        // pageSize: data.pageSize,
        total: data.totalRows,
        showTotal: () => {
            return `共${data.totalRows}条`
        },
        showQuickJumper: true,
        }
    },
    /**
     * ETable 行点击通用函数
     * @param {*选中行的索引} selectedRowKeys
     * @param {*选中行对象} selectedItem
     */
    updateSelectedItem(selectedRowKeys, selectedRows, selectedIds) {
        if (selectedIds) {
        this.setState({
            selectedRowKeys,
            selectedIds: selectedIds,
            selectedItem: selectedRows,
        })
        } else {
        this.setState({
            selectedRowKeys,
            selectedItem: selectedRows,
        })
        }
    },
    /**
     * ETable 换行列render
     */
    multLineCol(text, record) {
        if (text != null) {
        //   if (text.indexOf('@') != -1) {
        //     text = 'sztgzhiling@sz.icbc.com.cn;zhangjiangxin@sz.icbc.com.cn'
        //   }
        return <TableItemTip orgText={text} />
        }
    },

    //isEqual：判断两个对象是否键值对应相等
    isEqual(a,b){
            //如果a和b本来就全等
            if(a===b){
            //判断是否为0和-0
                return a !== 0 || 1/a ===1/b;
            }
            //判断是否为null和undefined
            if(a==null||b==null){
                return a===b;
            }
            //接下来判断a和b的数据类型
            var classNameA = typeof a,
            classNameB = typeof b;
            // var classNameA=toString.call(a),
            // classNameB=toString.call(b);
            //如果数据类型不相等，则返回false
            if(classNameA !== classNameB){
                return false;
            }
            //如果数据类型相等，再根据不同数据类型分别判断
            switch(classNameA){
                case '[object RegExp]':
                case '[object String]':
                //进行字符串转换比较
                return '' + a ==='' + b;
                case '[object Number]':
                //进行数字转换比较,判断是否为NaN
                if(+a !== +a){
                    return +b !== +b;
                }
                //判断是否为0或-0
                return +a === 0?1/ +a === 1/b : +a === +b;
                case '[object Date]':
                case '[object Boolean]':
                return +a === +b;
            }
            //如果是对象类型
            if(classNameA == '[object Object]'){
            //获取a和b的属性长度
            var propsA = Object.getOwnPropertyNames(a),
                propsB = Object.getOwnPropertyNames(b);
            if(propsA.length != propsB.length){
                return false;
            }
            for(var i=0;i<propsA.length;i++){
                var propName=propsA[i];
                //如果对应属性对应值不相等，则返回false
                if(a[propName] !== b[propName]){
                    return false;
                }
            }
            return true;
            }
            //如果是数组类型
            if(classNameA == '[object Array]'){
            if(a.toString() == b.toString()){
                return true;
            }
            return false;
        }
    }
  
}
class TableItemTip extends React.Component {
    constructor(props) {
      super(props)
      this.state = {}
      let text = props.orgText
      this.splitText(text);
    }

    componentWillReceiveProps(props){
        let text = props.orgText;
        let oldText = this.props.orgText;
        if(text === null || oldText === null || text === oldText) return;
        this.splitText(text);
    }
    splitText = (text)=>{
        if(text === null) return;
        text = text.replace(/[\[\],;]/g,(text)=> {
          switch (text) {
            case '[':
            case ']':
              return ''          
            default:
            return text+'|'
          }
        })
        let snArray = text.split('|')
        var showText = snArray[0].replace(/[\[\],;]/g,'')
        let hasMore = false;
        var result = snArray.map((text, index) => {
            if (index > 0) {
            if (text !== '') {
              hasMore = true;
            }
            return (
                <span key={index}>
                <br />
                {text}
                </span>
            )
            } else {
            return <span  key={index}>{text}</span>
            }
        })
        let state = {
            showText,
            contentHidden: snArray.length > 1 && hasMore,
            tipDom: <span>{result}</span>,
        }
        if (this.state.showText) {
            this.setState(state);
        }else{
            this.state = state;
        }
    }

    render() {
        const { tipDom, showText = '', contentHidden } = this.state
        let content = <span>{showText + (contentHidden ? '...' : '')}</span>
        return contentHidden ? <Tooltip title={tipDom}>{content}</Tooltip> : content
    }
}
